package com.example.supplychainbackend.entity.quality;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table(name = "inspection_order")
@Data
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class InspectionOrderEntity {
    @Id
    private String inspectionOrderNumber;

    private String inspectionRequestNumber;     //request
    private Integer inspectionType;
    private String containerNumber;             //batch
    private Integer inspectionQuantity;         //request
    private Integer qualifiedQuantity;          //dto
    private Integer unqualifiedQuantity;        //dto
    private Integer inspectionMethod;           //dto
    private String samplingStandard;            //dto
    private Integer referenceDocumentType;      //request
    private String externalDocumentNumber;      //request
    private String contractNumber;              //request
    private String contractName;                //request
    private String salesOrderNumber;            //request
    private String salesOrderName;              //request
    private String inspectionBatchNumber;       //batch
    private String referenceDocumentNumber;     //request
    private Integer sourceDocumentType;         //request
    private String sourceDocumentNumber;        //request
    private String sourceDocumentLineNumber;    //request
    private String materialNumber;              //request
    private String materialName;                //request
    private String materialDesc;         //request
    private String manufacturer;                //batch
    private String manufacturerMaterialCode;    //batch
    private String supplierCode;                //request
    private String supplierName;                //request
    private String inspector;                   //dto
    private String inspectionTime;              //dto
    private String requestor;                   //request
    private String requestDepartment;           //request
    private String requestTime;                 //request
    private Integer status;                     //request,need set
    private Integer inspectionConclusion;       //dto

    private String batchNumber;                 //request
    private String inspectionPlanName;          //dto
    private String inspectionPlanAttachment;    //dto
    private String executionStandard;           //dto

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "inspection_order_number")
    private List<InspectionOrderItemEntity> inspectionItems;

    //创建时间 用于溯源
    private String createDate;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @Transient
    private static AtomicInteger globalCounter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (this.inspectionOrderNumber == null || this.inspectionOrderNumber.isEmpty()) {
            // 原子递增获取序列号
            int sequence = globalCounter.getAndIncrement();

            // 格式化为IR+YYYYMMDD+五位序号
            this.inspectionOrderNumber = "IO" + DateTimeUtils.getCurrentDateForID() + String.format("%05d", sequence);
        }
        createDate = DateTimeUtils.getCurrentDateTime();
    }


}
